package library.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import library.dao.IlibraryDao;
import library.db.DbConnection;
import library.pojo.Book;
import library.pojo.BookType;
import library.pojo.BorrowHistory;
import library.pojo.User;
import library.pojo.UserType;

/**
 * @author zaizai
 *
 */
public class BorrowHistoryDao implements IlibraryDao {
	/**
	 * 修改借阅历史
	 */
	public void updateState(String isbn) {
		Connection conn = DbConnection.getConnection();
		String sql = "update borrow_history set borrow_state = ? where isbn = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, "归还");
			ps.setString(2, isbn);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 修改状态为续借
	 */
	public void updateRenewState(String isbn) {
		Connection conn = DbConnection.getConnection();
		String sql = "update borrow_history set borrow_state = ? where isbn = ? and borrow_state = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, "续借");
			ps.setString(2, isbn);
			ps.setString(3, "借出");
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 修改续借次数0
	 */
	public void updateCount0(String isbn) {
		Connection conn = DbConnection.getConnection();
		String sql = "update borrow_history set count = ? where isbn = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, 0);
			ps.setString(2, isbn);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}
	
	/**
	 * 修改续借次数1
	 */
	public void updateCount1(String isbn) {
		Connection conn = DbConnection.getConnection();
		String sql = "update borrow_history set count = ? where isbn = ? and count = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, 1);
			ps.setString(2, isbn);
			ps.setInt(3, 0);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 修改续借次数2
	 */
	public void updateCount2(String isbn) {
		Connection conn = DbConnection.getConnection();
		String sql = "update borrow_history set count = ? where isbn = ? and count = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, 2);
			ps.setString(2, isbn);
			ps.setInt(3, 1);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 修改续借次数3
	 */
	public void updateCount3(String isbn) {
		Connection conn = DbConnection.getConnection();
		String sql = "update borrow_history set count = ? where isbn = ? and count = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, 3);
			ps.setString(2, isbn);
			ps.setInt(3, 2);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 续借
	 */
	public void updateRemainTime(String isbn, int remainTime, String state) {
		Connection conn = DbConnection.getConnection();
		String sql = "update borrow_history set remain_time = ? where isbn = ? and borrow_state = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, remainTime);
			ps.setString(2, isbn);
			ps.setString(3, state);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	@Override
	public void save(Book book) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Book book) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Book> get(String isbn) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(String isbn) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Book> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addBookType(BookType bookType) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteBookType(String idx) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateBookType(BookType bookType) {
		// TODO Auto-generated method stub

	}

	@Override
	public BookType getBookType(String idx) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BookType> findAllBookType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteUser(String username) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub

	}

	@Override
	public User getUser(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findAllUser() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addUserType(UserType userType) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteUserType(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateUserType(UserType userType) {
		// TODO Auto-generated method stub

	}

	@Override
	public UserType getUserType(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UserType> findAllUserType() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 增加借阅历史
	 */
	@Override
	public void addBorrowHistory(BorrowHistory borrowHistory) {
		Connection conn = DbConnection.getConnection();
		String sql = "insert into borrow_history values(?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, borrowHistory.getIsbn());
			ps.setString(2, borrowHistory.getBookName());
			ps.setString(3, borrowHistory.getBorrowUser());
			ps.setDate(4, java.sql.Date.valueOf(borrowHistory.getBorrowDate()));
			ps.setInt(5, borrowHistory.getRemainTime());
			ps.setString(6, "借出");
			ps.setInt(7, borrowHistory.getCount());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 删除借阅历史
	 */
	@Override
	public void deleteBorrowHistory(String isbn, String borrowUser) {
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "delete from borrow_history where borrow_user = ?  and isbn = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, borrowUser);
			ps.setString(2, isbn);
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 修改借阅历史
	 */
	@Override
	public void updateBorrowHistory(BorrowHistory borrowHistory) {
		Connection conn = DbConnection.getConnection();
		String sql = "update borrow_history set isbn=?,book_name=?,borrow_date=? where borrow_user=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, borrowHistory.getIsbn());
			ps.setString(2, borrowHistory.getBookName());
			ps.setDate(3, java.sql.Date.valueOf(borrowHistory.getBorrowDate()));
			ps.setString(4, borrowHistory.getBorrowUser());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 查询指定借阅历史
	 */
	@Override
	public BorrowHistory getBorrowHistory(String borrowUser) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from  borrow_history where borrow_user=?";
		BorrowHistory borrowHistory = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, borrowUser);
			rs = ps.executeQuery();
			while (rs.next()) {
				String isbn = rs.getString(1);
				String bookName = rs.getString(2);
				String borrowUsers = rs.getString(3);
				String borrowDate = rs.getString(4);
				int time = rs.getInt(5);
				String borrowState = rs.getString(6);
				int count = rs.getInt(7);
				borrowHistory = new BorrowHistory(isbn, bookName, borrowUsers, borrowDate, borrowState, time, count);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return borrowHistory;
	}

	/**
	 * 查询所有借阅历史
	 */
	@Override
	public List<BorrowHistory> findAllBorrowHistory() {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from borrow_history";
		BorrowHistory borrowHistory = null;
		List<BorrowHistory> borrowHistoryList = new ArrayList<BorrowHistory>();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				String isbn = rs.getString(1);
				String bookName = rs.getString(2);
				String borrowUser = rs.getString(3);
				String borrowDate = rs.getString(4);
				int time = rs.getInt(5);
				String borrowState = rs.getString(6);
				int count = rs.getInt(7);
				borrowHistory = new BorrowHistory(isbn, bookName, borrowUser, borrowDate, borrowState, time, count);
				borrowHistoryList.add(borrowHistory);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return borrowHistoryList;
	}

	
	@Override
	public String getTypeName(String idx) {
		// TODO Auto-generated method stub
		return null;
	}

}
